import React, { Component } from 'react'
import { connect } from 'react-redux'
import { Form, Switch, Input, Radio, Button, message } from 'antd'
import { messages } from "share/common";
import Priority from "components/template/priority";
import InputLanguage from "components/template/input-language";
import SettingService from "../../finance-setting/loan-setting/setting-service"

@connect(mapStateToProps)
class TypeAdvancePayment extends Component {

  state = {
    priority: {}
  };

  componentWillMount() {
    const { record } = this.props.params, { company } = this.props;
    this.setState({
      priority: record.id ? {
        levelOrgName: record.levelOrgName,
        levelOrgId: record.setOfBooksId
      } : {
        levelOrgName: company.setOfBooksName,
        levelOrgId: company.setOfBooksId
      }
    })
  };

  handlePriority = value => {
    this.setState({ priority: value });
  };

  handleSave = () => {
    this.props.form.validateFieldsAndScroll((err, value) => {
      if (!err) {
        const { record } = this.props.params, { close } = this.props;
        const { priority } = this.state;
        let i18n = value.prepaymentTypeName;
        if (!priority || !priority.levelOrgId) {
          message.error(messages('finance-1.key371')/*请分配权限*/);
          return -1;
        }
        if (!i18n.name) {
          message.error(messages('finance-9.key81')/*预付款类型名称必填*/);
          return -1;
        }
        value.prepaymentTypeName = i18n.name;
        value.i18n = {
          prepaymentTypeName: i18n.i18n
        };
        value.setOfBooksId = priority.levelOrgId;
        this.setState({ saveLoading: true });
        SettingService.handleTypeRule(Object.assign(record, value)).then(() => {
          message.success(messages('common.operate.success')/*操作成功*/);
          this.setState({ saveLoading: true });
          close(true)
        }).catch(e => {
          this.setState({ saveLoading: false });
          message.error(e.response.data.message)
        })
      }
    })
  };

  render() {
    const { getFieldDecorator } = this.props.form;
    const { close } = this.props, { record } = this.props.params;
    const { priority, saveLoading } = this.state;
    const formItemLayout = {
      labelCol: {
        xs: { span: 24 },
        sm: { span: 6 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 10 },
      },
    };
    return (
      <div>
        <Form className={'new-form'}>
          <Form.Item
            label={messages('common.column.status')/*状态*/}
            {...formItemLayout}
          >
            {getFieldDecorator("enabled", {
              rules: [{ required: true }],
              valuePropName: "checked",
              initialValue: record.hasOwnProperty('enabled') ? record.enabled : true
            })(
              <Switch/>
            )}
          </Form.Item>
          <Priority
            top='SET_OF_BOOK'
            disabled={record.hasOwnProperty('id')}
            onChange={this.handlePriority}
            formItemLayout={formItemLayout}
            defaultValue={priority}
            single={false}
            showCompany={false}
            showCompanyComponent={false}
          />
          <Form.Item
            label={messages('finance-9.key82')/*预付款类型编码*/}
            {...formItemLayout}
          >
            {getFieldDecorator("prepaymentTypeCode", {
              rules: [{ required: true, message: messages('common.please.input')/*请输入*/ }],
              initialValue: record.prepaymentTypeCode || 'YF001'
            })(
              <Input/>
            )}
          </Form.Item>
          <Form.Item
            label={messages('finance-9.key83')/*预付款类型名称*/}
            {...formItemLayout}
          >
            {getFieldDecorator('prepaymentTypeName', {
              rules: [{ required: true, message: messages('common.please.input')/*请输入*/ }],
              initialValue: { name: record.prepaymentTypeName, i18n: record.i18n ? record.i18n.prepaymentTypeName : [] }
            })(
              <InputLanguage
                placeholder={messages('common.max.characters.length',{arg1: 100})/*最多输入{arg1}个字符*/}
                maxLength={100}
              />
            )}
          </Form.Item>
          <h3 className='sub-title'>{messages('finance-9.key84')/*核销管控*/}</h3>
          <Form.Item
            label={messages('finance-9.key85')/*强制核销*/}
            {...formItemLayout}
            extra={messages('finance-9.key86')/*启用后，有符合条件的预付款余额时，录入到票支付数据时会强制核销*/}
          >
            {getFieldDecorator("compulsoryWriteOff", {
              rules: [{ required: true }],
              initialValue: record.hasOwnProperty('compulsoryWriteOff') ? record.compulsoryWriteOff : false,
            })(
              <Radio.Group options={[
                { label: messages('common.status.enable')/*启用*/, value: true },
                { label: messages('finance-9.key87')/*不启用*/, value: false }
              ]}/>
            )}
          </Form.Item>
          <div className='footer-operate'>
            <Button type='primary' onClick={this.handleSave} loading={saveLoading}>{messages('common.ok')/*确定*/}</Button>
            <Button onClick={() => close()}>{messages('common.cancel')/*取消*/}</Button>
          </div>
        </Form>
      </div>
    )
  }

}

function mapStateToProps(state) {
  return {
    company: state.login.company
  }
}

export default Form.create()(TypeAdvancePayment)
